﻿@using StackExchange.Elastic
@model StackExchange.Opserver.Views.Elastic.DashboardModel
@{
    var c = Model.Current.Cluster;
    if (c == null) { return; }
    var hs = c.HealthStatus.SafeData(true);
    var warningsOnly = Model.WarningsOnly || Model.Current.WarningsOnly;
    var indices = (warningsOnly ? c.TroubledIndexes : c.HealthStatus.Data.Indices).ToList();
    var stats = c.Stats.SafeData(true);
}
@try
{
    if (indices.Any())
    {
        <table class="node-dashboard cluster-dashboard striped-dashboard">
            <tbody class="node-header" data-name="header-indexes-@c.UniqueKey-@hs.Name">
                <tr class="category-row">
                    <th colspan="10">
                        <h3>
                            @c.IconSpan() @indices.Count().ToComma() Indexes @(warningsOnly ? "in trouble" : "") on @hs.TotalNodeCount.Pluralize("node") (@hs.Name)
                            <span class="top-right-info">Updated @c.Stats.ToPollSpan()</span>
                        </h3>
                    </th>
                </tr>
                <tr>
                    <th></th>
                    <th>Name</th>
                    <th>Searches <span class="note">(cur)</span></th>
                    <th>Docs <span class="note">(deleted)</span></th>
                    <th>Size</th>
                    <th>Shards <span class="note" title="Replicas">(repl)</span></th>
                    <th>Active <span class="note" title="Primary Active Shards">(pri)</span></th>
                    <th title="Initializing">Init</th>
                    <th title="Relocating">Rel</th>
                    <th title="Unassigned">Un</th>
                </tr>
            </tbody>
            <tbody class="node-group" data-name="indexes-@c.UniqueKey-@hs.Name">
                @foreach (var i in indices.OrderByWorst().ThenBy(i =>
                {
                    int j;
                    return int.TryParse(i.Name, out j) ? j : 0;
                }).ThenBy(i => i.Name))
                {
                    IndexStats iStats;
                    stats.Indices.TryGetValue(i.Name, out iStats);
                    <tr class="@i.RowClass()">
                        <td>@i.IconSpan()</td>
                        <td>@c.GetIndexAliasedName(i.Name)</td>
                        @if (iStats != null)
                        {
                            <td>@(iStats.Total.Search.QueryTotal.ToComma()) <span class="note">(@(iStats.Total.Search.QueryCurrent.ToComma()))</span></td>
                            <td>@(iStats.Total.Documents.Count.ToComma()) <span class="note">(@(iStats.Total.Documents.Deleted.ToComma()))</span></td>
                            <td>@(iStats.Total.Store.SizeInBytes.ToSize("b"))</td>
                        }
                        else
                        {
                            <td colspan="3" class="note">(no stats)</td>
                        }
                        <td>@i.NumberOfShards.ToComma() <span class="note">(@i.NumberOfReplicas.ToComma())</span></td>
                        <td>@i.ActiveShards.ToComma() <span class="note">(@i.ActivePrimaryShards.ToComma())</span></td>
                        <td>@i.InitializingShards.ToComma()</td>
                        <td>@i.RelocatingShards.ToComma()</td>
                        <td>@i.UnassignedShards.ToComma()</td>
                    </tr>
                }
                @if (!warningsOnly)
                {
                    var indexDict = stats.GetIndexStats();
                    var indexStats = indexDict != null ? indexDict.Values : null;
                    <tr class="total-row">
                        <td></td>
                        <td><b>Total</b></td>
                        @if (indexStats != null)
                        {
                            <td>@(indexStats.Sum(i => i.Total.Search.QueryTotal).ToComma()) <span class="note">(@(indexStats.Sum(i => i.Total.Search.QueryCurrent).ToComma()))</span></td>
                            <td>@indexStats.Sum(i => i.Total.Documents.Count).ToComma() <span class="note">(@indexStats.Sum(i => i.Total.Documents.Deleted).ToComma())</span></td>
                            <td>@indexStats.Sum(i => i.Total.Store.SizeInBytes).ToSize("b")</td>
                        }
                        else
                        {
                            <td colspan="3" class="note">(no stats)</td>
                        }
                        <td>@indices.Sum(i => i.NumberOfShards)  <span class="note">(@indices.Sum(i => i.NumberOfReplicas))</span></td>
                        <td>@indices.Sum(i => i.ActiveShards) <span class="note">(@indices.Sum(i => i.ActivePrimaryShards))</span></td>
                        <td>@indices.Sum(i => i.InitializingShards)</td>
                        <td>@indices.Sum(i => i.RelocatingShards)</td>
                        <td>@indices.Sum(i => i.UnassignedShards)</td>
                    </tr>
                }
            </tbody>
        </table>
    }
    else if (!warningsOnly)
    {
        <div class="no-content">No indexes found in the @c.Name cluster</div>
    }
}
catch (Exception e)
{
    Current.LogException(e);
    <div class="no-content">Error showing indexes for @c.Name: @e.Message</div>
}